package com.bitcola.chainserver.server.inner;

import cn.hutool.core.util.RandomUtil;
import com.bitcola.chainserver.constant.DepositStatusConstant;
import com.bitcola.chainserver.server.BaseChainServer;
import com.bitcola.chainserver.server.trx.TrxServer;
import com.bitcola.security.common.msg.ColaChainBalance;
import com.bitcola.security.common.msg.ColaChainDepositResponse;
import com.bitcola.security.common.msg.ColaChainWithdrawResponse;
import com.bitcola.security.common.util.MD5Utils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
@Log4j2
public class InnerService  extends BaseChainServer {
    @Autowired
    TrxServer trxServer;
    volatile static long blockNumber=0;


    public long getCurrentBlockNumber()
    {
        try {
           return   trxServer.getCurrentNumber();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0L;
    }
    @Override
    public String getModuleName() {
        return "INNER";
    }

    @Override
    protected void run() throws Throwable {


    }

    @Override
    public String newAccount(String coinCode) throws Throwable {
        return null;
    }

    @Override
    public boolean checkAddress(String address) {
        return address.startsWith("lk");
    }

    @Override
    public ColaChainWithdrawResponse withdraw(String from ,String coinCode, String address, BigDecimal number, String memo) throws Throwable {
        ColaChainWithdrawResponse response = new ColaChainWithdrawResponse();
        response.setSuccess(true);
        response.setHash("0x"+ MD5Utils.MD5(System.currentTimeMillis()+""+ RandomUtil.randomInt(1,100000)));
        response.setFeeCoinCode(coinCode);
        response.setFee(BigDecimal.ZERO);
        ColaChainDepositResponse deposit = createDepositEntity(response.getHash(), number, coinCode, getModuleName(),
                DepositStatusConstant.NOT_CONFIRM, address, from,null,getCurrentBlockNumber());
        String orderId = deposit(deposit);
        if(orderId!=null) completeDeposit(orderId);
        return response;
    }

    @Override
    public void confirm() throws Throwable {

    }

    @Override
    public ColaChainBalance getChainBalance(String coinCode, String feeCoinCode) throws Throwable {
        return null;
    }
}
